package com.ruoyi.project.modules.businessSystem.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.project.modules.category.domain.Category;
import com.ruoyi.project.modules.category.mapper.CategoryMapper;
import com.ruoyi.project.modules.table.domain.TableInfo;
import com.ruoyi.project.modules.tableObject.domain.TableObject;
import com.ruoyi.project.modules.tableObject.mapper.TableObjectMapper;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.modules.businessSystem.mapper.BusinessSystemMapper;
import com.ruoyi.project.modules.businessSystem.domain.BusinessSystem;
import com.ruoyi.project.modules.businessSystem.service.IBusinessSystemService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 业务系统Service业务层处理
 *
 * @author smallrain
 * @date 2021-11-06
 */
@Service
public class BusinessSystemServiceImpl implements IBusinessSystemService {
    @Resource
    private BusinessSystemMapper businessSystemMapper;

    @Resource
    private CategoryMapper categoryMapper;

    /**
     * 查询业务系统
     *
     * @param id 业务系统主键
     * @return 业务系统
     */
    @Override
    public BusinessSystem selectBusinessSystemById(Long id) {
        return businessSystemMapper.selectBusinessSystemById(id);
    }

    /**
     * 查询业务系统列表
     *
     * @param businessSystem 业务系统
     * @return 业务系统
     */
    @Override
    public List<BusinessSystem> selectBusinessSystemList(BusinessSystem businessSystem) {
        return businessSystemMapper.selectBusinessSystemList(businessSystem);
    }

    /**
     * 新增业务系统
     *
     * @param businessSystem 业务系统
     * @return 结果
     */
    @Override
    public int insertBusinessSystem(BusinessSystem businessSystem) {
        businessSystem.setCreateTime(DateUtils.getNowDate());
        businessSystem.setCreateBy(ShiroUtils.getLoginName());
        if (businessSystem.getCategoryId() != null) {
            Category category = this.categoryMapper.selectCategoryById(businessSystem.getCategoryId());
            businessSystem.setCategoryCode(category.getCode());
        }
        int i = businessSystemMapper.insertBusinessSystem(businessSystem);
        insertTableObject(businessSystem.getId(), businessSystem.getTableIds());
        return i;
    }

    @Resource
    private TableObjectMapper tableObjectMapper;

    /**
     * 修改业务系统
     *
     * @param businessSystem 业务系统
     * @return 结果
     */
    @Override
    public int updateBusinessSystem(BusinessSystem businessSystem) {
        businessSystem.setUpdateTime(DateUtils.getNowDate());
        businessSystem.setUpdateBy(ShiroUtils.getLoginName());
        tableObjectMapper.deleteTableObjectByObjectId(Arrays.asList(businessSystem.getId()), "1");
        insertTableObject(businessSystem.getId(), businessSystem.getTableIds());
        if (businessSystem.getCategoryId() != null) {
            Category category = this.categoryMapper.selectCategoryById(businessSystem.getCategoryId());
            businessSystem.setCategoryCode(category.getCode());
        }
        return businessSystemMapper.updateBusinessSystem(businessSystem);
    }

    private void insertTableObject(Long id, List<Long> tableIds) {
        if (CollectionUtil.isNotEmpty(tableIds)) {
            List<TableObject> tableObjects = new ArrayList<>();
            for (Long tableId : tableIds) {
                TableObject tableObject = new TableObject();
                tableObject.setObjectType("1");
                tableObject.setObjectId(id);
                tableObject.setTableId(tableId);
                tableObjects.add(tableObject);
            }
            this.tableObjectMapper.batchInsert(tableObjects);
        }
    }

    /**
     * 批量删除业务系统
     *
     * @param ids 需要删除的业务系统主键
     * @return 结果
     */
    @Override
    public int deleteBusinessSystemByIds(String ids) {
        List<Long> longs = Convert.toLongList(ids);
        this.tableObjectMapper.deleteTableObjectByObjectId(longs, "1");
        return businessSystemMapper.deleteBusinessSystemByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除业务系统信息
     *
     * @param id 业务系统主键
     * @return 结果
     */
    @Override
    public int deleteBusinessSystemById(Long id) {
        return businessSystemMapper.deleteBusinessSystemById(id);
    }

    @Override
    public List<TableInfo> tableList(Set<Long> interfaceIds) {
        return businessSystemMapper.tableList(interfaceIds);
    }

    @Override
    public Set<Long> bffList(List<Long> tableIds) {
        return businessSystemMapper.bffList(tableIds);
    }
}
